package yhao.demo.service.person.service;

import org.springframework.stereotype.Service;
import yhao.demo.service.person.apilist.form.PersonEditForm;
import yhao.demo.service.person.apilist.form.PersonInsertForm;
import yhao.demo.service.person.apilist.form.PersonQueryForm;
import yhao.demo.service.person.apilist.model.PersonModel;
import yhao.demo.service.person.dao.PersonDao;
import yhao.infra.apilist.validate.IdForm;
import yhao.infra.common.model.Pagination;
import yhao.infra.common.util.MD5Util;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * 人员记录服务层Service
 * Created by yoara on 2019-03-28
 */
@Service
public class PersonService {
    @Resource
    private PersonDao personDao;

    public PersonModel selectById(IdForm form) {
        return personDao.selectById(form.getId());
    }

    public PersonInsertForm insertPerson(PersonInsertForm form) {
        form.setOperatorTime(new Date());
        form.setId(UUID.randomUUID().toString());
        form.setPassword(MD5Util.getMD5(form.getPassword().getBytes()));
        personDao.insertPerson(form);
        return form;
    }

    public PersonEditForm editPerson(PersonEditForm form) {
        form.setOperatorTime(new Date());
        form.setPassword(MD5Util.getMD5(form.getPassword().getBytes()));
        personDao.updatePerson(form);
        return form;
    }

    public Pagination<PersonModel> queryPersonPage(PersonQueryForm form) {
        Pagination<PersonModel> page = form.makePagination();
        personDao.queryPersonPage(page,form);
        return page;
    }

    public IdForm deleteById(IdForm form) {
        personDao.deleteById(form.getId());
        return form;
    }
}
